package com.cctc.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

import com.cctc.security.util.RequestUrl;

@Configuration
public class SpringBeanSecurity {

	private String SUCCESS_HANDLER_STRING = "/index.jsp";
	//private String SPRING_SECURITY_CHECK = "/j_spring_security_check";
	private String FAILURE_HANDLER_STRING = RequestUrl.LOGIN;

	//		@Bean
	//		public  FilterChainProxy  configureFilterChainProxy(){
	//			FilterChainProxy filterChainProxy=new FilterChainProxy();
	//			
	//		//	filterChainProxy.
	//			return null;
	//		}

	//	@Bean
	//	public UsernamePasswordAuthenticationFilter configureMyUsernamePasswordAuthenticationFilter() {
	//		UsernamePasswordAuthenticationFilter loginFilter = new MyUsernamePasswordAuthenticationFilter();
	//		loginFilter.setFilterProcessesUrl(SPRING_SECURITY_CHECK);
	//		loginFilter.setAuthenticationSuccessHandler(configureSavedRequestAwareAuthenticationSuccessHandler());
	//		loginFilter.setAuthenticationFailureHandler(configureSimpleUrlAuthenticationFailureHandler());
	//		//loginFilter.setAuthenticationManager(authenticationManager);
	//		return loginFilter;
	//	}

	@Bean(name = "loginLogAuthenticationSuccessHandler")
	public SavedRequestAwareAuthenticationSuccessHandler configureSavedRequestAwareAuthenticationSuccessHandler() {
		SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
		handler.setDefaultTargetUrl(SUCCESS_HANDLER_STRING);
		return handler;
	}

	@Bean(name = "simpleUrlAuthenticationFailureHandler")
	public SimpleUrlAuthenticationFailureHandler configureSimpleUrlAuthenticationFailureHandler() {
		SimpleUrlAuthenticationFailureHandler handler = new SimpleUrlAuthenticationFailureHandler();
		handler.setDefaultFailureUrl(FAILURE_HANDLER_STRING);
		return handler;
	}

	//	@Bean
	//	public SecurityContextPersistenceFilter configureSecurityContextPersistenceFilter() {
	//		return new SecurityContextPersistenceFilter();
	//	}

	@Bean(name = "authenticationProcessingFilterEntryPoint")
	public LoginUrlAuthenticationEntryPoint configureLoginUrlAuthenticationEntryPoint() {
		//LoginUrlAuthenticationEntryPoint lae = new LoginUrlAuthenticationEntryPoint();
		//lae.setLoginFormUrl(FAILURE_HANDLER_STRING);
		return new LoginUrlAuthenticationEntryPoint(FAILURE_HANDLER_STRING);
	}

	//	@Bean
	//	public AuthenticationManager configureAuthenticationManager() {
	//		AuthenticationManager manager = new ProviderManager();
	//		//manager.authenticate(authentication);
	//		return manager;
	//	}

	//	@Bean
	//	public FilterSecurityInterceptor configureFilterSecurityInterceptor() {
	//		FilterSecurityInterceptor filterSecurityInterceptor = new MyFilterSecurityInterceptor();
	//		filterSecurityInterceptor.setAccessDecisionManager(new MyAccessDecisionManager());
	//		filterSecurityInterceptor.setAuthenticationManager(configureAuthenticationManager());
	//		filterSecurityInterceptor.setSecurityMetadataSource(new MyInvocationSecurityMetadataSourceService());
	//		return filterSecurityInterceptor;
	//	}

	//	@Bean
	//	public AnonymousAuthenticationFilter filterAnonymousAuthenticationFilter() {
	//		return new AnonymousAuthenticationFilter();
	//	}
}
